package com.zhhtech.zhh.ui.dialog

import android.content.Context
import com.contrarywind.view.WheelView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopupext.adapter.ArrayWheelAdapter
import com.zhhtech.zhh.ui.R
import com.zhhtech.zhh.ui.databinding.PopOptionPickerBinding
import com.zhhtech.zhh.ui.ext.toColor

class OptionPickerPopup(context: Context, data: List<Any>? = null, var optionPickerListener: ((Any) -> Unit)? = null) : BottomPopupView(context) {
    companion object {
        fun create(context: Context, data: List<Any>? = null, optionPickerListener: ((Any) -> Unit)? = null) =
            XPopup.Builder(context)
                .asCustom(OptionPickerPopup(context, data, optionPickerListener))
                    as OptionPickerPopup
    }

    private val datas = mutableListOf<Any>()

    init {
        data?.apply { datas.addAll(this) }
    }

    override fun getImplLayoutId() = R.layout.pop_option_picker
    override fun onCreate() {
        super.onCreate()
        PopOptionPickerBinding.bind(this).apply {
            btnCancel.setOnClickListener { dismiss() }
            btnConfirm.setOnClickListener {
                if (options1.currentItem < datas.size) {
                    optionPickerListener?.invoke(datas[options1.currentItem])
                }
                dismiss()
            }
            options1.adapter = ArrayWheelAdapter(datas)
            options1.currentItem = 0
            options1.setTextSize(18f)
            options1.setItemsVisibleCount(7)
            options1.setAlphaGradient(true)
            options1.setCyclic(false)

            options1.setDividerColor(R.color.line.toColor())
            options1.setDividerType(WheelView.DividerType.FILL)
            options1.setLineSpacingMultiplier(3f) // 条目间距倍数 默认2
            options1.setTextColorOut(R.color.text_hint_color_2.toColor())
            options1.setTextColorCenter(R.color.text_color.toColor())
            options1.isCenterLabel(false)
        }
    }
}